package com.haisen.controller;


import com.haisen.core.annotation.SystemLog;
import com.haisen.core.utils.PageResultSet;
import com.haisen.core.utils.Result;
import com.haisen.dataobject.entity.Bill;
import com.haisen.dataobject.entity.User;
import com.haisen.dataobject.query.BillQuery;
import com.haisen.service.BillCategroyService;
import com.haisen.service.BillService;
import com.haisen.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Controller
@RequestMapping("/finance/bill")
public class BillController {

    @Autowired
    private BillService billService;
    @Autowired
    private BillCategroyService billCategroyService;
    @Autowired
    private UserService userService;

    @GetMapping
    @RequiresPermissions("bill:view")
    public String page(Model model){
        setCommData(model);
        return "finance/bill";
    }

    @ResponseBody
    @PostMapping("/create")
    @RequiresPermissions("bill:create")
    @SystemLog("用户创建费用")
    public Result create(@Valid Bill bill){
        // 当前用户
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        User user = userService.findByUsername(username);
        billService.create(bill);
        return Result.success();
    }

    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions("bill:update")
    @SystemLog("修改费用")
    public Result update(@Valid Bill bill){
        billService.update(bill);
        return Result.success();
    }

    @ResponseBody
    @PostMapping("/delete")
    @RequiresPermissions("bill:delete")
    @SystemLog("删除费用")
    public Result delete(@RequestParam("id") Long id, HttpServletRequest request){
          billService.delete(id);
         return Result.success();
    }

    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("bill:view")
    public PageResultSet<Bill> findByPage(BillQuery billQuery){
       return billService.findByPage(billQuery);
    }

    private void setCommData(Model model) {
       model.addAttribute("catgoryList",billCategroyService.findAll());
    }


}
